<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box{
            width: 100px;
            height: 100px;
            background: red;
            position: absolute;
            left: 100px;
            top:100px
        }
    </style>
</head>
<body>
    <div class="box"></div>
    <script>
        var boxEle = document.querySelector(".box");
        // boxEle.onmousedown = function(event){
        //     var e = event || window.event;
        //     var x = e.clientX;  //A 距离 左边浏览器的距离
        //     var y = e.clientY;
        //     var offsetLeftNum = boxEle.offsetLeft;
        //     var offsetTopNum = boxEle.offsetTop;

        //     boxEle.onmousemove = function(event){
        //         var e = event || window.event;
        //         var xx = e.clientX; // B 距离左边浏览器的距离
        //         var yy = e.clientY;
        //         var disx = xx - x + offsetLeftNum;
        //         var disy = yy - y + offsetTopNum;
        //         boxEle.style.left = disx + "px";
        //         boxEle.style.top = disy + "px";
        //     }

        //     // 当鼠标抬起的时候 清空掉  move事件
        //     // 只有在box上抬起的时候才会清空事件；
        //     document.onmouseup = function(){
        //         boxEle.onmousemove = "";
        //     }
        // }


        boxEle.onmousedown = function(event){
            var e = event || window.event;
            var x = e.clientX  - this.offsetLeft;
            var y = e.clientY - this.offsetTop ;
            this.onmousemove = function(event){
                var e = event || window.event;
                var xx = e.clientX;
                var yy = e.clientY;
                this.style.left = xx - x  + "px";
                this.style.top  = yy - y +  "px";
            }
            var that = this;
            document.onmouseup = function(){
                that.onmousemove = "";
            }
        }









    </script>
</body>
</html>